package com.sr.vo.req;

import com.sr.pojo.WMember;
import com.sr.util.ToolUtil;
import com.sr.vo.BaseForm;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * @author xiaochi
 * @date 2022/4/30 11:47
 * @desc MemberReqVo
 */
@ApiModel("会员实体")
@Data
@EqualsAndHashCode(callSuper = true)
public class MemberReqVo extends BaseForm<WMember> {

    @ApiModelProperty("更新时必须")
    @Size(min = 19,max = 19,message = "id不正确",groups = Update.class)
    @NotBlank(message = "id不能为空",groups = Update.class)
    private String id;

    @ApiModelProperty("用户账号")
    @NotBlank(message = "账号不能为空",groups = Create.class)
    @Length(min = 8,max = 16,message = "账号为{min} ~ {max}位",groups = Create.class)
    private String username;

    @ApiModelProperty("用户密码")
    @NotBlank(message = "密码不能玩为空",groups = {Create.class,Update.class})
    @Length(min = 8,max = 16,message = "密码为{min} ~ {max}位",groups = {Create.class,Update.class})
    private String password;

    @ApiModelProperty("用户昵称")
    @NotBlank(message = "昵称不能为空",groups = {Create.class,Update.class})
    @Length(min = 1,max = 10,message = "昵称长度为{min} ~ {max}位",groups = {Create.class,Update.class})
    private String nickname;

    @ApiModelProperty("用户头像,不超过255个字符")
    @Length(min = 0,max = 255,message = "头像地址长度为{min} ~ {max}位",groups = {Create.class,Update.class})
    private String head;

    @ApiModelProperty("用户手机号")
    @Length(min = 11,max = 11,message = "手机不正确",groups = {Create.class,Update.class})
    @Pattern(regexp = "1[3|4|5|7|8][0-9]\\d{8}",message = "手机不正确",groups = {Create.class,Update.class})
    private String phone;

    @ApiModelProperty("邮箱长度为 8 ~ 16 位")
    @Length(min = 8,max = 16,message = "邮箱长度为 8 ~ 16 位",groups = {Create.class,Update.class})
    private String email;

    @ApiModelProperty("QQ长度为 8 ~ 16 位")
    @Length(min = 8,max = 16,message = "QQ长度为 8 ~ 16 位",groups = {Create.class,Update.class})
    private String qq;

    @ApiModelProperty("身份证长度为 8 ~ 16 位")
    @Length(min = 18,max = 20,message = "身份证长度为 8 ~ 16 位",groups = {Create.class,Update.class})
    private String idCard;

    @ApiModelProperty("省名称长度为 1 ~ 32 位")
    @Length(min = 1,max = 32,message = "省名称长度为 1 ~ 32 位",groups = {Create.class,Update.class})
    private String provinceName;

    @ApiModelProperty("省id长度为 1 ~ 10 位")
    @Length(min = 1,max = 10,message = "省id长度为 1 ~ 10 位",groups = {Create.class,Update.class})
    private String provinceId;

    @ApiModelProperty("会员级别id长度为 1 ~ 32 位")
    @Length(min = 1,max = 32,message = "会员级别id长度为 1 ~ 32 位",groups = {Create.class,Update.class})
    private String memberLevelId;

    @ApiModelProperty("用户积分")
    @PositiveOrZero(message = "用户积分必须是正整数",groups = {Create.class,Update.class})
    private Long integral;

    @ApiModelProperty("自我介绍长度为 0 ~ 255 位")
    @Length(max = 32,message = "自我介绍长度为 0 ~ 255 位",groups = {Create.class,Update.class})
    private String selfDesc;

    @ApiModelProperty("会员状态")
    @Range(min = 0,max = 2,message = "状态为{min} ~ {max}",groups = {Select.class})
    private Integer isDel;

    @Override
    public WMember buildEntity() {
        WMember wMember = ToolUtil.copy(this, WMember.class);
        Date date = new Date();
        if (StringUtils.isEmpty(wMember.getId())){
            wMember.setId(ToolUtil.nextId());
            wMember.setCreateTime(date);
            wMember.setUpdateTime(date);
        }else{
            wMember.setUpdateTime(date);
        }
        return wMember;
    }
}
